package edu.liu.db;

import javax.swing.*;
import java.sql.*;

import javax.swing.tree.*;


public class DBBrowserUI extends JFrame
{
	
	JTree jt;
	DatabaseMetaData dmd;
	
	
	public void initDBMetaData() throws Exception
	{
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://www.objectgraph.com:6464;DatabaseName=JavaTest","javatest","javatest");
		dmd=con.getMetaData();
	}
	
	
	public DBBrowserUI()
	{
		try
		{
			initDBMetaData();
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.toString());
			return;
		}
		
		String databaseName="Root";
		
		try
		{
			databaseName=dmd.getDatabaseProductName();
		}
		catch(Exception ex)
		{
			
		}
		
		DefaultMutableTreeNode root=new DefaultMutableTreeNode(databaseName);
		getCatalogs(root);
		
		jt=new JTree(root);
		JScrollPane jsp=new JScrollPane(jt);
		getContentPane().add(jsp,"Center");
		
		
		setTitle("DB Browser 1.0");
		setSize(500,500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
	}
	
	public void getCatalogs(DefaultMutableTreeNode root)
	{
		try
		{
			ResultSet catalogs=dmd.getCatalogs();
			while(catalogs.next())
			{
				String catalog=catalogs.getString("TABLE_CAT");
				DefaultMutableTreeNode catalogNode=new DefaultMutableTreeNode(catalog);
				getSchemas(catalogNode,catalog);
				root.add(catalogNode);
			}
		
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.toString());
		}
	}
	
	public void getSchemas(DefaultMutableTreeNode catalog,String catalogName) 
	{
		try
		{
			ResultSet schemas=dmd.getSchemas();
			while(schemas.next())
			{
				String schema=schemas.getString("TABLE_SCHEM");
				DefaultMutableTreeNode schemaNode=new DefaultMutableTreeNode(schema);
				getTables(schemaNode,catalogName,schema);
				catalog.add(schemaNode);
			}
		
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.toString());
		}
	}
	
	public void getTables(DefaultMutableTreeNode schemaNode,String catalog,String schema) 
	{
		try
		{
			ResultSet tables=dmd.getTables(catalog,	schema,null,null);
			while(tables.next())
			{
				String tableName=tables.getString("TABLE_NAME");
				DefaultMutableTreeNode tableNode=new DefaultMutableTreeNode(tableName);
				//getColumns(tableNode,"JavaTest",schema,tableName);
				schemaNode.add(tableNode);
			}
		
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.toString());
		}
	}
	
	public void getColumns(DefaultMutableTreeNode tableNode,String catalog,String schema,String table) 
	{
		try
		{
			ResultSet columns=dmd.getColumns(catalog,schema,table,null);
			while(columns.next())
			{
				String columnName=columns.getString("COLUMN_NAME")+" "+columns.getString("TYPE_NAME");
				DefaultMutableTreeNode columnNode=new DefaultMutableTreeNode(columnName);
				tableNode.add(columnNode);
			}
		
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.toString());
		}
	}
	
	
	public static void main(String[] args)
	{
		new DBBrowserUI();
	}
}
